package com.fairytask.engine.components.tree
{
	import com.adobe.utils.StringUtil;
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.Scroll.ScrollPanel;
	import com.fairytask.engine.components.Scroll.ScrollSkin;
	import com.fairytask.managers.ResManager;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	
	public class Tree extends BaseComponent
	{
		//显示
		private var _sPanel		:ScrollPanel;
		private var _treeSpace		:Sprite	=	new Sprite();	//Tree显示区域
		private var _selectMc		:Shape	=	new Shape();
		private var _width			:int;
		private var _height		:int;
		private var _canWrap		:Boolean	=	false;
		//数据
		private var _dataArr		:Array	=	[];
		private var _selectItem	:TreeItem	=	null;
		private var _hideRoot		:Boolean;	//是否显示不存在子项的跟目录
		private var _hideSelect	:Boolean;	//是否需要选中状态
		private var _rootBgStr		:String = "";	//根项背景资源路径
		private var _layOut		:String = "left";
		private var _treeBg		:Object;		//tree背景    可以是string,从UI库中提取，也可以是自己绘制的显示对象
		private const SELECT_COLOR	:uint	=	0xFF9900;
		
		public function Tree(_w:int,_h:int,_sk:String,hideRoot:Boolean=false,hideSelect:Boolean=false,rootBgStr:String = "",layOut:String = "left",treeBg:Object=null)
		{
			_skin	=	_sk;
			_width	=	_w;
			_height	=	_h;
			_layOut = layOut;
			if(rootBgStr != "")
			{
				_rootBgStr = rootBgStr;
			}
			//树背景
			treeBackGround	=	treeBg;
					
			//初始化显示
			//选中显示
			_hideRoot	=	hideRoot;
			_hideSelect	=	hideSelect;
			_selectMc.graphics.beginFill(SELECT_COLOR);
			_selectMc.graphics.drawRect(0,2,_width-30,16);
			_selectMc.graphics.endFill();
			//滚动条
			_sPanel	=	new ScrollPanel(new Rectangle(0,0,_width,_height),_treeSpace,new ScrollSkin("vScrol_1","vScrol_7"));
			addChild(_sPanel);
			initEvent();
		}
		
		/**
		 *	设置Tree组件的背景 
		 * @param value
		 * 
		 */		
		public function set treeBackGround(value:Object):void
		{
			if(value==null)
			{
				return;
			}
			
			if(value is String)
			{
				var bg:DisplayObject = ResManager.newResClass(String(value)) as DisplayObject;
				bg.cacheAsBitmap;
				bg.y = 1;
				_treeBg	=	bg;
			}
			else if(value is DisplayObject)
			{
				_treeBg	=	value;	//自定义图形
			}
			_treeBg.width		=	_width;	
			_treeBg.height	=	_height;
			addChild(DisplayObject(_treeBg));		
		}
		
		private function initEvent():void
		{
			addEventListener(TreeEvent.CLICK_ICON, onIconClick);
			addEventListener(TreeEvent.CLICK_ITEM, onItemClick);
		}
		
		private function onItemClick(evt:TreeEvent):void
		{
			if(evt.item.parentNote!=null)
			{
				_selectItem	=	evt.item;	//选择子项
				_selectMc.height	=	_selectItem.height;
				_selectMc.width		=	_selectItem.width + 2;
				selectedPos(evt.item);//选中效果位置
				if(!_hideSelect)		//
				{
					_treeSpace.addChildAt(_selectMc,0);//添加选中效果  于最底层
				}
				dispatchEvent(new TreeEvent(TreeEvent.SHOW_MENU,evt.item));
			}
			else
			{
				_selectItem	=	null;
			}
		}
		
		/**
		 *	获取选择子项 
		 * @return 
		 * 
		 */		
		public function get selectItem():TreeItem
		{
			return _selectItem;
		}
		
		/**
		 *	选中效果位置 
		 * @param item
		 * 
		 */		
		public function selectedPos(item:TreeItem):void
		{
//			if(_layOut == "left")
//			{
//				_selectMc.x	=	item.parentNote.x+item.x;
//			}else
//			{
				_selectMc.x	=	item.nameTxt.x - 1;
//			}
			_selectMc.y	=	item.parentNote.y+item.y-2;
		}
		
		public function get selectMcPos():Array
		{
			var tp:Array = new Array();
			tp.push(_selectMc.x);
			tp.push(_selectMc.y);
			return tp;
		}
		
		/**
		 *	Tree节点伸缩 
		 * @param evt
		 * 
		 */		
		private function onIconClick(evt:TreeEvent):void
		{
			refreshView();//刷新显示
			if(_selectItem==null)
			{
				if(_selectMc.parent!=null)
				{
					removeChild(_selectMc);
				}
				return;
			}
			if(evt.item.hasItem(_selectItem))
			{
				if(evt.item.isOpen)
				{
					selectedPos(_selectItem);
				}
				else
				{
					if(_selectMc.parent!=null)
					{
						removeChild(_selectMc);
					}
					dispatchEvent(new TreeEvent(TreeEvent.CLICK_ITEM,evt.item));
				}
			}
			else
			{
				selectedPos(_selectItem);
			}
		}
		
		/**
		 *	获取子项数据 
		 * @return 
		 * 
		 */		
		public function  get dataArr():Array
		{
			return _dataArr;
		}
		/**
		 *	为树添加子元素 
		 * @param str		显示名称
		 * @param pNote		父节点
		 * @param id		子项ID
		 * @param data		子元素数据
		 * @param time		显示计时
		 * @param color		字体颜色
		 * res/png/task/Task_Colse.png
		 */		
		public function addItem(str:String,pNote:int=-1,id:int=-1,data:Array=null,pIconStr:String="",time:int=0,timeIcon:String = "",color:int=0):void
		{
			if(pNote==-1)
			{
				var tmpItem	:TreeItem	=	new TreeItem(str,_skin,true,false,"",0,"",_rootBgStr,_layOut);
				tmpItem.id		=	_dataArr.length;	//父项的序列
				tmpItem.data	=	data;
				_dataArr.push(tmpItem);
			}
			else
			{
				if(time != 0)
				{
					if(color!=0)
					{
						_dataArr[pNote].addItem(str,this,id,data,true,pIconStr,time,timeIcon,color,canWrap,_width-30);
					}
					else
					{
						_dataArr[pNote].addItem(str,this,id,data,true,pIconStr,time,timeIcon,0xFFCB4A,canWrap,_width-30);
					}
				}else
				{
					if(color!=0)
					{
						_dataArr[pNote].addItem(str,this,id,data,true,pIconStr,0,"",color,canWrap,_width-30);
					}
					else
					{
						_dataArr[pNote].addItem(str,this,id,data,true,pIconStr,0,"",0xFFCB4A,canWrap,_width-30);
					}
				}
				
			}
			refreshView();
			_sPanel.update();
		}
		
		/**
		 * 通知树项ID来删除 
		 * @param id
		 * 
		 */
		public function removeItemById(id:int):void
		{
			var renItem	:TreeItem	=	getItemById(id);
			if(null!=renItem&&null!=renItem.parentNote)
			{
				renItem.parentNote.removeItem(renItem);
			}
			refreshView();
			_sPanel.update();
		}
		
		/**
		 * 根据节点和ID来删除树项 
		 * @param index
		 * @param id
		 * 
		 */
		public function removeItem(index:int,id:int):void
		{
			if(index<0)return;
			_dataArr[index].removeItemById(id);
			refreshView();
			_sPanel.update();
		}
		
		/**
		 * 更新显示对象 
		 */
		private function refreshView():void
		{
			//清空显示容器
			while(_treeSpace.numChildren>0)
			{
				_treeSpace.removeChildAt(0);
			}
			var tmpHeight	:int	=	0;
			for each(var item:TreeItem in _dataArr)
			{
				if((true==_hideRoot)&&(item.childArr.length==0))
				{
					continue;
				}
				item.y	=	tmpHeight;
				tmpHeight	+=	item.height;
				_treeSpace.addChild(item);
			}
		}
		/**
		 * 清空List中的数据 
		 * 
		 */		
		public function removeAll():void
		{
			_dataArr=[];
		}
		
		public function clear():void
		{
			_dataArr=[];
			while(_treeSpace.numChildren>0)
			{
				_treeSpace.removeChildAt(0);
			}
		}
		
		public function removeStatus():void
		{
			if(_selectMc.parent!=null)
			{
				_treeSpace.removeChild(_selectMc);
			}
		}
		
		/**
		 * 通过ID来取得树项 
		 * @param id
		 * @return 
		 * 
		 */
		public function getItemById(id:int):TreeItem
		{
			var renItem	:TreeItem	=	null;
			for each(var item:TreeItem in _dataArr)
			{
				if(0	==	item.childArr.length)
				{
					continue;					
				}
				for each(var childItem:TreeItem in item.childArr)
				{
					if(childItem.id==id)
					{
						renItem	=	childItem;
					}
				}
			}
			return renItem;
		}
		
		
		/**
		 *	通过Id选择子项 
		 * @param id
		 * 
		 */		
		public function selectItemById(id:int):void
		{
			for each(var item:TreeItem in _dataArr)
			{
				if(0	==	item.childArr.length)
				{
					continue;					
				}
				for each(var childItem:TreeItem in item.childArr)
				{
					if(childItem.id==id)
					{
						item.dispatchEvent(new TreeEvent(TreeEvent.CLICK_ICON,item,item.data));
						this.dispatchEvent(new TreeEvent(TreeEvent.CLICK_ITEM,childItem,childItem.data));
					}
				}
			}
		}
		
		public function selectItemByData(data:String):void
		{
			for each(var item:TreeItem in _dataArr)
			{
				if(0	==	item.childArr.length)
				{
					continue;					
				}
				for each(var childItem:TreeItem in item.childArr)
				{
					if(StringUtil.contains((childItem.htmlText),data)>0)
					{
						item.dispatchEvent(new TreeEvent(TreeEvent.CLICK_ICON,item,item.data));
						this.dispatchEvent(new TreeEvent(TreeEvent.CLICK_ITEM,childItem,childItem.data));
					}
				}
			}
			
		}
		
		/**
		 * 重制tree高度 
		 * @param val
		 * 
		 */
		public function set reHeight(val:Number):void
		{
			_height = val;
			_sPanel.resize(new Rectangle(0,0,_width,_height));
		}

		public function get canWrap():Boolean
		{
			return _canWrap;
		}

		public function set canWrap(value:Boolean):void
		{
			_canWrap = value;
		}

		
	}
}